#include <stdio.h>
#include "mvos/mvos_apis.h"
#include "test_apis.h"

static mv_s32_t __ai_data_push_cb(MVP_AUDIO_TYEP_E e_type, mv_u8ptr_t frame_data, mv_s32_t frame_len)
{
#if 0
    static FILE *fp = NULL;
    static mv_s32_t cnt = 0;

    if(fp == NULL && cnt == 0) {
        fp = fopen("/tmp/test.aac", "w+");
    }

    if(fp){
        fwrite(frame_data, 1, frame_len, fp);
        if(cnt++ > 50) {
            fclose(fp);
            fp = NULL;
        }
    }
#endif

    // MVPC_LOG_INF("[AUDIO] len:%d \n", frame_len);
    return MV_EC_SUCCESS;
}

mv_s32_t test_audio_ao_start()
{
    MVP_AUDIO_ATTR_T ao_attr = {0};
    ao_attr.sample_rate      = MV_AUDIO_SAMPLE_RATE_8000_P;
    ao_attr.sample_bits      = MV_AUDIO_SMPLE_BIT_16_P;
    ao_attr.channel_num      = MV_AUDIO_CHANNEL_MONO_P;
    ao_attr.audio_type       = MV_AUDIO_TYPE_AAC_P;
    ao_attr.aec_eanble       = MV_TRUE;
    ao_attr.nr_enable        = MV_TRUE;
    ao_attr.agc_eanble       = MV_TRUE;
    ao_attr.audio_codec_type = MV_AUDIO_HARD_CODEC;
    mvp_ao_init(&ao_attr);

    if(mvos_access("/mnt/sdcard/sf_reset_cn.mp3", MVOS_F_OK) == 0) {
        mvp_ao_play_file("/mnt/sdcard/sf_reset_cn.mp3", 1);
    }

    return MV_EC_SUCCESS;
}

mv_s32_t test_audio_ao_stop()
{
    mvp_ao_uninit();
    return MV_EC_SUCCESS;
}

mv_s32_t test_audio_ai_start()
{
    MVP_AUDIO_ATTR_T ai_attr = {0};
    ai_attr.sample_rate      = MV_AUDIO_SAMPLE_RATE_8000_P;
    ai_attr.sample_bits      = MV_AUDIO_SMPLE_BIT_16_P;
    ai_attr.channel_num      = MV_AUDIO_CHANNEL_MONO_P;
    ai_attr.audio_type       = MV_AUDIO_TYPE_AAC_P;
    ai_attr.nr_enable        = MV_TRUE;
    ai_attr.agc_eanble       = MV_TRUE;
    ai_attr.aec_eanble       = MV_TRUE;
    ai_attr.audio_codec_type = MV_AUDIO_HARD_CODEC;
    
    mvp_ai_init(&ai_attr);
    mvp_aenc_start(__ai_data_push_cb, NULL);
    mvp_ai_volume_set(80);

    return MV_EC_SUCCESS;
}

mv_s32_t test_audio_ai_stop()
{
    mvp_aenc_stop();
    mvp_ai_uninit();
    return MV_EC_SUCCESS;
}